package com.qf.controller.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Product;
import com.qf.service.product.ProductService;
import com.qf.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 永无霸哥 ---
 *
 * @author M.rk
 * @date 2024/12/3 15:25
 * @desc
 */
@RestController
@RequestMapping("/drugProd")
@Slf4j
public class ProductController {

    @Autowired
    ProductService productService;

    //查询所有、模糊查询、分页
    @GetMapping("/list")
    public R productList(Product product) {

        PageHelper.startPage(product.getPageNum(), product.getPageSize());

        List<Product> list = productService.productList(product);


        System.out.println("products ====> " + list);

        String path = "https://mrk-zz.oss-cn-beijing.aliyuncs.com";

        String unite;

        for (Product product1 : list) {
//            拿到每个对象的TradeChart值
            String tradeChart = product1.getTradeChart();
            unite = path + tradeChart;
            product1.setTradeChart(unite);
        }
        PageInfo<Product> PageInfo = new PageInfo(list);
        return R.ok(PageInfo);
    }


    //新增商品数据
    //TODO 待完成创建时间、修改时间、还有图片的格式问题
    @PostMapping("/addrug")
    public R adddrug(@RequestBody Product product) {

        log.debug("获得的对象是-----》" + product);
        int i = productService.adddrug(product);

        if (i > 0) {
            return R.ok();
        }

        return R.fail();
    }


    //单个删除
    @GetMapping("/handleDelete")
    public R handleDelete(int id) {
        int i = productService.handleDelete(id);

        if (i > 0) {
            return R.ok();
        }

        return R.fail();
    }

    //批量删除
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam ArrayList<Integer> ids) {
        int i = productService.delBatch(ids);

        if (i > 0) {
            return R.ok();
        }

        return R.fail();
    }

    //编辑商品信息
    //TODO：修改时间等
    @PostMapping("/redact")
    public R redactProduct(@RequestBody Product product) {
        int i = productService.redactProduct(product);

        if (i > 0) {
            return R.ok();
        }

        return R.fail();
    }


}
